package com.huawei.zyb.taskManager.infrastructure.persistence.repository;

import com.huawei.zyb.taskManager.domain.developer.entity.Developer;
import com.huawei.zyb.taskManager.domain.developer.repository.DeveloperRepository;
import com.huawei.zyb.taskManager.domain.developer.valueobject.*;
import com.huawei.zyb.taskManager.infrastructure.persistence.mapper.DeveloperMapper;
import com.huawei.zyb.taskManager.infrastructure.persistence.po.DeveloperPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 开发者仓储实现
 */
@Repository
public class DeveloperRepositoryImpl implements DeveloperRepository {
    
    @Autowired
    private DeveloperMapper developerMapper;
    
    @Override
    public Developer save(Developer developer) {
        DeveloperPO developerPO = toDeveloperPO(developer);
        if (developerPO.getId() == null || !developerMapper.existsById(developerPO.getId())) {
            developerMapper.insert(developerPO);
        } else {
            developerMapper.update(developerPO);
        }
        return developer;
    }
    
    @Override
    public Optional<Developer> findById(DeveloperId developerId) {
        DeveloperPO developerPO = developerMapper.findById(developerId.getValue());
        return developerPO != null ? Optional.of(toDeveloper(developerPO)) : Optional.empty();
    }
    

    
    @Override
    public List<Developer> findByRole(DeveloperRole role) {
        List<DeveloperPO> developerPOs = developerMapper.findByRole(role.name());
        return developerPOs.stream().map(this::toDeveloper).collect(Collectors.toList());
    }
    
    @Override
    public List<Developer> findByStatus(DeveloperStatus status) {
        List<DeveloperPO> developerPOs = developerMapper.findByStatus(status.name());
        return developerPOs.stream().map(this::toDeveloper).collect(Collectors.toList());
    }
    
    @Override
    public List<Developer> findAll() {
        List<DeveloperPO> developerPOs = developerMapper.findAll();
        return developerPOs.stream().map(this::toDeveloper).collect(Collectors.toList());
    }
    
    @Override
    public List<Developer> findByNameContaining(String namePattern) {
        List<DeveloperPO> developerPOs = developerMapper.findByNameLike(namePattern);
        return developerPOs.stream().map(this::toDeveloper).collect(Collectors.toList());
    }
    
    @Override
    public List<Developer> findAvailableDevelopers() {
        List<DeveloperPO> developerPOs = developerMapper.findAvailableForAssignment();
        return developerPOs.stream().map(this::toDeveloper).collect(Collectors.toList());
    }
    
    @Override
    public List<Developer> findManagementRoleDevelopers() {
        List<DeveloperPO> developerPOs = developerMapper.findManagementRoles();
        return developerPOs.stream().map(this::toDeveloper).collect(Collectors.toList());
    }
    
    @Override
    public void deleteById(DeveloperId developerId) {
        developerMapper.deleteById(developerId.getValue());
    }
    
    @Override
    public boolean existsById(DeveloperId developerId) {
        return developerMapper.existsById(developerId.getValue());
    }
    

    
    @Override
    public boolean hasIncompleteTasks(DeveloperId developerId) {
        return developerMapper.hasIncompleteTasks(developerId.getValue());
    }
    
    /**
     * 将领域对象转换为持久化对象
     */
    private DeveloperPO toDeveloperPO(Developer developer) {
        DeveloperPO developerPO = new DeveloperPO();
        if (developer.getId() != null) {
            developerPO.setId(developer.getId().getValue());
        }
        developerPO.setName(developer.getName().getValue());
        developerPO.setRole(developer.getRole().name());
        developerPO.setStatus(developer.getStatus().name());
        developerPO.setCreatedAt(developer.getCreatedAt());
        developerPO.setUpdatedAt(developer.getUpdatedAt());
        return developerPO;
    }
    
    /**
     * 将持久化对象转换为领域对象
     */
    private Developer toDeveloper(DeveloperPO developerPO) {
        return Developer.restore(
            DeveloperId.of(developerPO.getId()),
            DeveloperName.of(developerPO.getName()),
            DeveloperRole.valueOf(developerPO.getRole()),
            DeveloperStatus.valueOf(developerPO.getStatus()),
            developerPO.getCreatedAt(),
            developerPO.getUpdatedAt()
        );
    }
}